const express = require('express');
const router = express.Router();

// 模拟分类数据
const mockCategoryData = {
  // 完整的分类树数据
  categoryTree: [
    {
      id: 1,
      pid: 0,
      name: '手机',
      title: '手机',
      text: 'iPhone/华为/小米/OPPO/vivo/三星',
      icon: '/static/images/home/goods/1.png',
      type: 'digital',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 101, pid: 1, name: 'iPhone', title: 'iPhone', text: '苹果手机系列', icon: '/static/images/home/grid-icon/30.png', type: 'digital', hot: false, recommend: true },
        { id: 102, pid: 1, name: '华为', title: '华为', text: '华为手机系列', icon: '/static/images/home/grid-icon/31.png', type: 'digital', hot: true, recommend: false },
        { id: 103, pid: 1, name: '小米', title: '小米', text: '小米手机系列', icon: '/static/images/home/grid-icon/32.png', type: 'digital', hot: true, recommend: false },
        { id: 104, pid: 1, name: 'OPPO', title: 'OPPO', text: 'OPPO手机系列', icon: '/static/images/home/grid-icon/33.png', type: 'digital', hot: false, recommend: true },
        { id: 105, pid: 1, name: 'vivo', title: 'vivo', text: 'vivo手机系列', icon: '/static/images/home/grid-icon/34.png', type: 'digital', hot: false, recommend: true },
        { id: 106, pid: 1, name: '三星', title: '三星', text: '三星手机系列', icon: '/static/images/home/grid-icon/35.png', type: 'digital', hot: true, recommend: false }
      ]
    },
    {
      id: 2,
      pid: 0,
      name: '图书影音',
      title: '图书影音',
      text: '文学小说/教材教辅/唱片影片',
      icon: '/static/images/home/goods/2.png',
      type: 'culture',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 201, pid: 2, name: '文学小说', title: '文学小说', text: '小说/散文/诗歌等文学作品', icon: '/static/images/home/grid-icon/45.png', type: 'culture', hot: true, recommend: false },
        { id: 202, pid: 2, name: '教材教辅', title: '教材教辅', text: '教材/教辅/考试用书', icon: '/static/images/home/grid-icon/46.png', type: 'culture', hot: false, recommend: true },
        { id: 203, pid: 2, name: '唱片影片', title: '唱片影片', text: 'CD/DVD/蓝光等音像制品', icon: '/static/images/home/grid-icon/47.png', type: 'culture', hot: false, recommend: true }
      ]
    },
    {
      id: 3,
      pid: 0,
      name: '数码',
      title: '数码',
      text: '摄影器材/耳机/智能穿戴/音箱/游戏机',
      icon: '/static/images/home/goods/3.png',
      type: 'digital',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 301, pid: 3, name: '摄影器材', title: '摄影器材', text: '相机/镜头/摄影配件', icon: '/static/images/home/grid-icon/35.png', type: 'digital', hot: true, recommend: false },
        { id: 302, pid: 3, name: '耳机', title: '耳机', text: '有线/无线耳机', icon: '/static/images/home/grid-icon/34.png', type: 'digital', hot: true, recommend: true },
        { id: 303, pid: 3, name: '智能穿戴', title: '智能穿戴', text: '智能手表/手环', icon: '/static/images/home/grid-icon/34.png', type: 'digital', hot: false, recommend: true },
        { id: 304, pid: 3, name: '音箱', title: '音箱', text: '蓝牙/智能音箱', icon: '/static/images/home/grid-icon/33.png', type: 'digital', hot: false, recommend: false },
        { id: 305, pid: 3, name: '游戏机', title: '游戏机', text: '游戏主机/掌机', icon: '/static/images/home/grid-icon/32.png', type: 'digital', hot: true, recommend: false }
      ]
    },
    {
      id: 4,
      pid: 0,
      name: '服装鞋帽',
      title: '服装鞋帽',
      text: '男装/女装/鞋/箱包/配饰',
      icon: '/static/images/home/goods/4.png',
      type: 'clothing',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 401, pid: 4, name: '男装', title: '男装', text: '男士服装/外套/裤子', icon: '/static/images/home/grid-icon/40.png', type: 'clothing', hot: true, recommend: false },
        { id: 402, pid: 4, name: '女装', title: '女装', text: '女士服装/裙子/上衣', icon: '/static/images/home/grid-icon/41.png', type: 'clothing', hot: true, recommend: true },
        { id: 403, pid: 4, name: '鞋靴', title: '鞋靴', text: '运动鞋/皮鞋/靴子', icon: '/static/images/home/grid-icon/42.png', type: 'clothing', hot: false, recommend: true },
        { id: 404, pid: 4, name: '箱包', title: '箱包', text: '背包/手提包/行李箱', icon: '/static/images/home/grid-icon/43.png', type: 'clothing', hot: false, recommend: false },
        { id: 405, pid: 4, name: '配饰', title: '配饰', text: '帽子/围巾/皮带等', icon: '/static/images/home/grid-icon/44.png', type: 'clothing', hot: false, recommend: true }
      ]
    },
    {
      id: 5,
      pid: 0,
      name: '交通工具',
      title: '交通工具',
      text: '摩托车/电动车/自行车/汽车用品/汽车配件',
      icon: '/static/images/home/goods/5.png',
      type: 'vehicle',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 501, pid: 5, name: '摩托车', title: '摩托车', text: '摩托车及配件', icon: '/static/images/home/grid-icon/54.png', type: 'vehicle', hot: true, recommend: false },
        { id: 502, pid: 5, name: '电动车', title: '电动车', text: '电动车及配件', icon: '/static/images/home/grid-icon/53.png', type: 'vehicle', hot: true, recommend: false },
        { id: 503, pid: 5, name: '自行车', title: '自行车', text: '自行车及配件', icon: '/static/images/home/grid-icon/52.png', type: 'vehicle', hot: false, recommend: true },
        { id: 504, pid: 5, name: '汽车用品', title: '汽车用品', text: '汽车内饰/清洁用品', icon: '/static/images/home/grid-icon/55.png', type: 'vehicle', hot: false, recommend: false },
        { id: 505, pid: 5, name: '汽车配件', title: '汽车配件', text: '汽车零部件/配件', icon: '/static/images/home/grid-icon/55.png', type: 'vehicle', hot: true, recommend: false }
      ]
    },
    {
      id: 6,
      pid: 0,
      name: '母婴用品',
      title: '母婴用品',
      text: '服饰/童车童床/玩具图书/洗护用品/孕妈用品',
      icon: '/static/images/home/goods/6.png',
      type: 'baby',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 601, pid: 6, name: '童装', title: '童装', text: '儿童服装/婴儿装', icon: '/static/images/home/grid-icon/48.png', type: 'baby', hot: true, recommend: false },
        { id: 602, pid: 6, name: '童车童床', title: '童车童床', text: '婴儿车/儿童床', icon: '/static/images/home/grid-icon/51.png', type: 'baby', hot: false, recommend: true },
        { id: 603, pid: 6, name: '玩具图书', title: '玩具图书', text: '儿童玩具/图书', icon: '/static/images/home/grid-icon/49.png', type: 'baby', hot: true, recommend: false },
        { id: 604, pid: 6, name: '洗护用品', title: '洗护用品', text: '婴儿洗护用品', icon: '/static/images/home/grid-icon/50.png', type: 'baby', hot: false, recommend: true },
        { id: 605, pid: 6, name: '孕妈用品', title: '孕妈用品', text: '孕妇用品/护理', icon: '/static/images/home/grid-icon/50.png', type: 'baby', hot: false, recommend: false }
      ]
    },
    {
      id: 7,
      pid: 0,
      name: '家用电器',
      title: '家用电器',
      text: '生活电器/厨房电器/电器配件',
      icon: '/static/images/home/goods/7.png',
      type: 'appliance',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 701, pid: 7, name: '生活电器', title: '生活电器', text: '小家电/生活电器', icon: '/static/images/home/grid-icon/38.png', type: 'appliance', hot: true, recommend: false },
        { id: 702, pid: 7, name: '厨房电器', title: '厨房电器', text: '厨房电器/厨具', icon: '/static/images/home/grid-icon/37.png', type: 'appliance', hot: true, recommend: true },
        { id: 703, pid: 7, name: '电器配件', title: '电器配件', text: '电器配件/耗材', icon: '/static/images/home/grid-icon/39.png', type: 'appliance', hot: false, recommend: false }
      ]
    },
    {
      id: 8,
      pid: 0,
      name: '家居家具',
      title: '家居家具',
      text: '沙发桌椅/家装软饰/灯具照明/厨房卫浴',
      icon: '/static/images/home/goods/8.png',
      type: 'furniture',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 801, pid: 8, name: '沙发桌椅', title: '沙发桌椅', text: '沙发/桌椅/家具', icon: '/static/images/home/grid-icon/56.png', type: 'furniture', hot: true, recommend: false },
        { id: 802, pid: 8, name: '家装软饰', title: '家装软饰', text: '装饰品/软装', icon: '/static/images/home/grid-icon/59.png', type: 'furniture', hot: false, recommend: true },
        { id: 803, pid: 8, name: '灯具照明', title: '灯具照明', text: '灯具/照明设备', icon: '/static/images/home/grid-icon/58.png', type: 'furniture', hot: false, recommend: false },
        { id: 804, pid: 8, name: '厨房卫浴', title: '厨房卫浴', text: '厨房/卫浴用品', icon: '/static/images/home/grid-icon/57.png', type: 'furniture', hot: true, recommend: true }
      ]
    },
    {
      id: 9,
      pid: 0,
      name: '运动户外',
      title: '运动户外',
      text: '健身器材/运动服饰/户外装备/球类运动',
      icon: '/static/images/home/goods/1.png',
      type: 'sports',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 901, pid: 9, name: '健身器材', title: '健身器材', text: '健身器械/器材', icon: '/static/images/home/grid-icon/62.png', type: 'sports', hot: true, recommend: false },
        { id: 902, pid: 9, name: '运动服饰', title: '运动服饰', text: '运动服装/鞋帽', icon: '/static/images/home/grid-icon/60.png', type: 'sports', hot: true, recommend: true },
        { id: 903, pid: 9, name: '户外装备', title: '户外装备', text: '户外运动装备', icon: '/static/images/home/grid-icon/61.png', type: 'sports', hot: false, recommend: false },
        { id: 904, pid: 9, name: '球类运动', title: '球类运动', text: '球类运动器材', icon: '/static/images/home/grid-icon/60.png', type: 'sports', hot: false, recommend: true }
      ]
    },
    {
      id: 10,
      pid: 0,
      name: '美妆个护',
      title: '美妆个护',
      text: '护肤品/彩妆/香水/个人护理/美发护发',
      icon: '/static/images/home/goods/2.png',
      type: 'beauty',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 1001, pid: 10, name: '护肤品', title: '护肤品', text: '面部护理/护肤品', icon: '/static/images/home/grid-icon/39.png', type: 'beauty', hot: true, recommend: false },
        { id: 1002, pid: 10, name: '彩妆', title: '彩妆', text: '化妆品/彩妆', icon: '/static/images/home/grid-icon/39.png', type: 'beauty', hot: true, recommend: true },
        { id: 1003, pid: 10, name: '香水', title: '香水', text: '香水/香氛', icon: '/static/images/home/grid-icon/39.png', type: 'beauty', hot: false, recommend: false },
        { id: 1004, pid: 10, name: '个人护理', title: '个人护理', text: '个人护理用品', icon: '/static/images/home/grid-icon/39.png', type: 'beauty', hot: false, recommend: true },
        { id: 1005, pid: 10, name: '美发护发', title: '美发护发', text: '美发/护发产品', icon: '/static/images/home/grid-icon/39.png', type: 'beauty', hot: false, recommend: false }
      ]
    },
    {
      id: 11,
      pid: 0,
      name: '电脑办公',
      title: '电脑办公',
      text: '笔记本电脑/台式机/平板电脑/办公设备',
      icon: '/static/images/home/goods/3.png',
      type: 'computer',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 1101, pid: 11, name: '笔记本电脑', title: '笔记本电脑', text: '笔记本电脑/超极本', icon: '/static/images/home/grid-icon/32.png', type: 'computer', hot: true, recommend: false },
        { id: 1102, pid: 11, name: '台式机', title: '台式机', text: '台式电脑/主机', icon: '/static/images/home/grid-icon/32.png', type: 'computer', hot: false, recommend: true },
        { id: 1103, pid: 11, name: '平板电脑', title: '平板电脑', text: '平板电脑/平板', icon: '/static/images/home/grid-icon/31.png', type: 'computer', hot: true, recommend: false },
        { id: 1104, pid: 11, name: '办公设备', title: '办公设备', text: '打印机/扫描仪等', icon: '/static/images/home/grid-icon/33.png', type: 'computer', hot: false, recommend: true }
      ]
    },
    {
      id: 12,
      pid: 0,
      name: '珠宝首饰',
      title: '珠宝首饰',
      text: '项链/戒指/手链/耳饰/手表/珠宝',
      icon: '/static/images/home/goods/4.png',
      type: 'jewelry',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 1201, pid: 12, name: '项链', title: '项链', text: '项链/吊坠', icon: '/static/images/home/grid-icon/44.png', type: 'jewelry', hot: true, recommend: false },
        { id: 1202, pid: 12, name: '戒指', title: '戒指', text: '戒指/指环', icon: '/static/images/home/grid-icon/44.png', type: 'jewelry', hot: true, recommend: true },
        { id: 1203, pid: 12, name: '手链', title: '手链', text: '手链/手镯', icon: '/static/images/home/grid-icon/44.png', type: 'jewelry', hot: false, recommend: false },
        { id: 1204, pid: 12, name: '耳饰', title: '耳饰', text: '耳环/耳钉', icon: '/static/images/home/grid-icon/44.png', type: 'jewelry', hot: false, recommend: true },
        { id: 1205, pid: 12, name: '手表', title: '手表', text: '手表/腕表', icon: '/static/images/home/grid-icon/44.png', type: 'jewelry', hot: true, recommend: false },
        { id: 1206, pid: 12, name: '珠宝', title: '珠宝', text: '珠宝/宝石', icon: '/static/images/home/grid-icon/44.png', type: 'jewelry', hot: false, recommend: true }
      ]
    },
    {
      id: 13,
      pid: 0,
      name: '食品生鲜',
      title: '食品生鲜',
      text: '休闲零食/生鲜果蔬/酒水饮料/粮油调味',
      icon: '/static/images/home/goods/5.png',
      type: 'food',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 1301, pid: 13, name: '休闲零食', title: '休闲零食', text: '零食/小吃', icon: '/static/images/home/grid-icon/55.png', type: 'food', hot: true, recommend: false },
        { id: 1302, pid: 13, name: '生鲜果蔬', title: '生鲜果蔬', text: '水果/蔬菜', icon: '/static/images/home/grid-icon/55.png', type: 'food', hot: true, recommend: true },
        { id: 1303, pid: 13, name: '酒水饮料', title: '酒水饮料', text: '酒类/饮料', icon: '/static/images/home/grid-icon/55.png', type: 'food', hot: false, recommend: false },
        { id: 1304, pid: 13, name: '粮油调味', title: '粮油调味', text: '粮油/调味品', icon: '/static/images/home/grid-icon/55.png', type: 'food', hot: false, recommend: true }
      ]
    },
    {
      id: 14,
      pid: 0,
      name: '宠物用品',
      title: '宠物用品',
      text: '宠物食品/宠物玩具/宠物服饰/宠物医疗',
      icon: '/static/images/home/goods/6.png',
      type: 'pet',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 1401, pid: 14, name: '宠物食品', title: '宠物食品', text: '宠物粮食/零食', icon: '/static/images/home/grid-icon/51.png', type: 'pet', hot: true, recommend: false },
        { id: 1402, pid: 14, name: '宠物玩具', title: '宠物玩具', text: '宠物玩具/用品', icon: '/static/images/home/grid-icon/49.png', type: 'pet', hot: false, recommend: true },
        { id: 1403, pid: 14, name: '宠物服饰', title: '宠物服饰', text: '宠物服装/饰品', icon: '/static/images/home/grid-icon/48.png', type: 'pet', hot: false, recommend: false },
        { id: 1404, pid: 14, name: '宠物医疗', title: '宠物医疗', text: '宠物医疗/护理', icon: '/static/images/home/grid-icon/50.png', type: 'pet', hot: true, recommend: true }
      ]
    },
    {
      id: 15,
      pid: 0,
      name: '乐器',
      title: '乐器',
      text: '钢琴/吉他/小提琴/管乐器/打击乐器',
      icon: '/static/images/home/goods/7.png',
      type: 'instrument',
      hot: true,
      recommend: false,
      subCategories: [
        { id: 1501, pid: 15, name: '钢琴', title: '钢琴', text: '钢琴/键盘乐器', icon: '/static/images/home/grid-icon/47.png', type: 'instrument', hot: true, recommend: false },
        { id: 1502, pid: 15, name: '吉他', title: '吉他', text: '吉他/弦乐器', icon: '/static/images/home/grid-icon/47.png', type: 'instrument', hot: true, recommend: true },
        { id: 1503, pid: 15, name: '小提琴', title: '小提琴', text: '小提琴/弦乐器', icon: '/static/images/home/grid-icon/47.png', type: 'instrument', hot: false, recommend: false },
        { id: 1504, pid: 15, name: '管乐器', title: '管乐器', text: '管乐器/吹奏乐器', icon: '/static/images/home/grid-icon/47.png', type: 'instrument', hot: false, recommend: true },
        { id: 1505, pid: 15, name: '打击乐器', title: '打击乐器', text: '打击乐器/鼓类', icon: '/static/images/home/grid-icon/47.png', type: 'instrument', hot: false, recommend: false }
      ]
    },
    {
      id: 16,
      pid: 0,
      name: '收藏品',
      title: '收藏品',
      text: '邮票/钱币/古董/艺术品/纪念品',
      icon: '/static/images/home/goods/8.png',
      type: 'collection',
      hot: false,
      recommend: true,
      subCategories: [
        { id: 1601, pid: 16, name: '邮票', title: '邮票', text: '邮票/邮品', icon: '/static/images/home/grid-icon/45.png', type: 'collection', hot: true, recommend: false },
        { id: 1602, pid: 16, name: '钱币', title: '钱币', text: '钱币/货币', icon: '/static/images/home/grid-icon/45.png', type: 'collection', hot: true, recommend: true },
        { id: 1603, pid: 16, name: '古董', title: '古董', text: '古董/文物', icon: '/static/images/home/grid-icon/45.png', type: 'collection', hot: false, recommend: false },
        { id: 1604, pid: 16, name: '艺术品', title: '艺术品', text: '艺术品/工艺品', icon: '/static/images/home/grid-icon/45.png', type: 'collection', hot: false, recommend: true },
        { id: 1605, pid: 16, name: '纪念品', title: '纪念品', text: '纪念品/收藏品', icon: '/static/images/home/grid-icon/45.png', type: 'collection', hot: false, recommend: false }
      ]
    }
  ]
};

// 获取完整的分类层级数据
router.post('/getCategoryTree', (req, res) => {
  try {
    res.json({
      code: 200,
      message: '成功',
      data: mockCategoryData.categoryTree
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

// 获取热门分类
router.post('/getHotCategories', (req, res) => {
  try {
    const hotCategories = mockCategoryData.categoryTree
      .filter(category => category.hot)
      .map(category => ({
        id: category.id,
        name: category.name,
        title: category.title,
        text: category.text,
        icon: category.icon,
        type: category.type,
        hot: true
      }));

    res.json({
      code: 200,
      message: '成功',
      data: hotCategories
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

// 获取推荐分类
router.post('/getRecommendedCategories', (req, res) => {
  try {
    const recommendedCategories = mockCategoryData.categoryTree
      .filter(category => category.recommend)
      .map(category => ({
        id: category.id,
        name: category.name,
        title: category.title,
        text: category.text,
        icon: category.icon,
        type: category.type,
        recommend: true,
        desc: category.text
      }));

    res.json({
      code: 200,
      message: '成功',
      data: recommendedCategories
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

// 获取分类列表数据（兼容旧接口）
router.post('/sortListData', (req, res) => {
  try {
    const hotCategories = mockCategoryData.categoryTree
      .filter(category => category.hot)
      .map(category => ({
        id: category.id,
        name: category.name,
        img: category.icon
      }));

    res.json({
      code: 200,
      message: '成功',
      data: hotCategories
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

// 根据分类ID获取子分类
router.post('/getSubCategories', (req, res) => {
  try {
    const { categoryId } = req.body;
    const category = mockCategoryData.categoryTree.find(cat => cat.id === parseInt(categoryId));

    if (!category) {
      return res.status(404).json({
        code: 404,
        message: '分类不存在',
        data: null
      });
    }

    res.json({
      code: 200,
      message: '成功',
      data: category.subCategories || []
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

// 根据分类类型获取分类
router.post('/getCategoriesByType', (req, res) => {
  try {
    const { type } = req.body;
    const categories = mockCategoryData.categoryTree.filter(category => category.type === type);

    res.json({
      code: 200,
      message: '成功',
      data: categories
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: '服务器错误',
      data: null
    });
  }
});

module.exports = router;